
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>cleverhans.attacks.elastic_net_method &#8212; CleverHans  documentation</title>
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/alabaster.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
   
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for cleverhans.attacks.elastic_net_method</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;The ElasticNetMethod attack.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="c1"># pylint: disable=missing-docstring</span>
<span class="kn">import</span> <span class="nn">logging</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">tensorflow</span> <span class="k">as</span> <span class="nn">tf</span>

<span class="kn">from</span> <span class="nn">cleverhans.attacks.attack</span> <span class="kn">import</span> <span class="n">Attack</span>
<span class="kn">from</span> <span class="nn">cleverhans.compat</span> <span class="kn">import</span> <span class="n">reduce_sum</span><span class="p">,</span> <span class="n">reduce_max</span>
<span class="kn">from</span> <span class="nn">cleverhans.model</span> <span class="kn">import</span> <span class="n">Model</span><span class="p">,</span> <span class="n">CallableModelWrapper</span><span class="p">,</span> <span class="n">wrapper_warning_logits</span>
<span class="kn">from</span> <span class="nn">cleverhans</span> <span class="kn">import</span> <span class="n">utils</span>

<span class="n">np_dtype</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;float32&#39;</span><span class="p">)</span>
<span class="n">tf_dtype</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">as_dtype</span><span class="p">(</span><span class="s1">&#39;float32&#39;</span><span class="p">)</span>

<span class="n">_logger</span> <span class="o">=</span> <span class="n">utils</span><span class="o">.</span><span class="n">create_logger</span><span class="p">(</span><span class="s2">&quot;cleverhans.attacks.elastic_net_method&quot;</span><span class="p">)</span>
<span class="n">_logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">ZERO</span><span class="p">():</span>
  <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="mf">0.</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np_dtype</span><span class="p">)</span>


<div class="viewcode-block" id="ElasticNetMethod"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.ElasticNetMethod">[docs]</a><span class="k">class</span> <span class="nc">ElasticNetMethod</span><span class="p">(</span><span class="n">Attack</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">  This attack features L1-oriented adversarial examples and includes</span>
<span class="sd">  the C&amp;W L2 attack as a special case (when beta is set to 0).</span>
<span class="sd">  Adversarial examples attain similar performance to those</span>
<span class="sd">  generated by the C&amp;W L2 attack in the white-box case,</span>
<span class="sd">  and more importantly, have improved transferability properties</span>
<span class="sd">  and complement adversarial training.</span>
<span class="sd">  Paper link: https://arxiv.org/abs/1709.04114</span>

<span class="sd">  :param model: cleverhans.model.Model</span>
<span class="sd">  :param sess: tf.Session</span>
<span class="sd">  :param dtypestr: dtype of the data</span>
<span class="sd">  :param kwargs: passed through to super constructor</span>
<span class="sd">  &quot;&quot;&quot;</span>

  <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">sess</span><span class="p">,</span> <span class="n">dtypestr</span><span class="o">=</span><span class="s1">&#39;float32&#39;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Note: the model parameter should be an instance of the</span>
<span class="sd">    cleverhans.model.Model abstraction provided by CleverHans.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">Model</span><span class="p">):</span>
      <span class="n">wrapper_warning_logits</span><span class="p">()</span>
      <span class="n">model</span> <span class="o">=</span> <span class="n">CallableModelWrapper</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="s1">&#39;logits&#39;</span><span class="p">)</span>

    <span class="nb">super</span><span class="p">(</span><span class="n">ElasticNetMethod</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">sess</span><span class="p">,</span> <span class="n">dtypestr</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">feedable_kwargs</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="s1">&#39;y_target&#39;</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">structural_kwargs</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s1">&#39;beta&#39;</span><span class="p">,</span> <span class="s1">&#39;decision_rule&#39;</span><span class="p">,</span> <span class="s1">&#39;batch_size&#39;</span><span class="p">,</span> <span class="s1">&#39;confidence&#39;</span><span class="p">,</span>
        <span class="s1">&#39;targeted&#39;</span><span class="p">,</span> <span class="s1">&#39;learning_rate&#39;</span><span class="p">,</span> <span class="s1">&#39;binary_search_steps&#39;</span><span class="p">,</span>
        <span class="s1">&#39;max_iterations&#39;</span><span class="p">,</span> <span class="s1">&#39;abort_early&#39;</span><span class="p">,</span> <span class="s1">&#39;initial_const&#39;</span><span class="p">,</span> <span class="s1">&#39;clip_min&#39;</span><span class="p">,</span>
        <span class="s1">&#39;clip_max&#39;</span>
    <span class="p">]</span>

<div class="viewcode-block" id="ElasticNetMethod.generate"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.ElasticNetMethod.generate">[docs]</a>  <span class="k">def</span> <span class="nf">generate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return a tensor that constructs adversarial examples for the given</span>
<span class="sd">    input. Generate uses tf.py_func in order to operate over tensors.</span>

<span class="sd">    :param x: (required) A tensor with the inputs.</span>
<span class="sd">    :param kwargs: See `parse_params`</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">sess</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span> \
        <span class="s1">&#39;Cannot use `generate` when no `sess` was provided&#39;</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">parse_params</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="n">labels</span><span class="p">,</span> <span class="n">nb_classes</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_or_guess_labels</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span>

    <span class="n">attack</span> <span class="o">=</span> <span class="n">EAD</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sess</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">beta</span><span class="p">,</span>
                 <span class="bp">self</span><span class="o">.</span><span class="n">decision_rule</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">confidence</span><span class="p">,</span>
                 <span class="s1">&#39;y_target&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">learning_rate</span><span class="p">,</span>
                 <span class="bp">self</span><span class="o">.</span><span class="n">binary_search_steps</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_iterations</span><span class="p">,</span>
                 <span class="bp">self</span><span class="o">.</span><span class="n">abort_early</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_const</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span><span class="p">,</span>
                 <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span><span class="p">,</span> <span class="n">nb_classes</span><span class="p">,</span>
                 <span class="n">x</span><span class="o">.</span><span class="n">get_shape</span><span class="p">()</span><span class="o">.</span><span class="n">as_list</span><span class="p">()[</span><span class="mi">1</span><span class="p">:])</span>

    <span class="k">def</span> <span class="nf">ead_wrap</span><span class="p">(</span><span class="n">x_val</span><span class="p">,</span> <span class="n">y_val</span><span class="p">):</span>
      <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">attack</span><span class="o">.</span><span class="n">attack</span><span class="p">(</span><span class="n">x_val</span><span class="p">,</span> <span class="n">y_val</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">np_dtype</span><span class="p">)</span>

    <span class="n">wrap</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">py_func</span><span class="p">(</span><span class="n">ead_wrap</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">labels</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">tf_dtype</span><span class="p">)</span>
    <span class="n">wrap</span><span class="o">.</span><span class="n">set_shape</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">get_shape</span><span class="p">())</span>

    <span class="k">return</span> <span class="n">wrap</span></div>

<div class="viewcode-block" id="ElasticNetMethod.parse_params"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.ElasticNetMethod.parse_params">[docs]</a>  <span class="k">def</span> <span class="nf">parse_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                   <span class="n">y</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="n">y_target</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="n">beta</span><span class="o">=</span><span class="mf">1e-2</span><span class="p">,</span>
                   <span class="n">decision_rule</span><span class="o">=</span><span class="s1">&#39;EN&#39;</span><span class="p">,</span>
                   <span class="n">batch_size</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
                   <span class="n">confidence</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
                   <span class="n">learning_rate</span><span class="o">=</span><span class="mf">1e-2</span><span class="p">,</span>
                   <span class="n">binary_search_steps</span><span class="o">=</span><span class="mi">9</span><span class="p">,</span>
                   <span class="n">max_iterations</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span>
                   <span class="n">abort_early</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                   <span class="n">initial_const</span><span class="o">=</span><span class="mf">1e-3</span><span class="p">,</span>
                   <span class="n">clip_min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
                   <span class="n">clip_max</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    :param y: (optional) A tensor with the true labels for an untargeted</span>
<span class="sd">              attack. If None (and y_target is None) then use the</span>
<span class="sd">              original labels the classifier assigns.</span>
<span class="sd">    :param y_target: (optional) A tensor with the target labels for a</span>
<span class="sd">              targeted attack.</span>
<span class="sd">    :param beta: Trades off L2 distortion with L1 distortion: higher</span>
<span class="sd">                 produces examples with lower L1 distortion, at the</span>
<span class="sd">                 cost of higher L2 (and typically Linf) distortion</span>
<span class="sd">    :param decision_rule: EN or L1. Select final adversarial example from</span>
<span class="sd">                          all successful examples based on the least</span>
<span class="sd">                          elastic-net or L1 distortion criterion.</span>
<span class="sd">    :param confidence: Confidence of adversarial examples: higher produces</span>
<span class="sd">                       examples with larger l2 distortion, but more</span>
<span class="sd">                       strongly classified as adversarial.</span>
<span class="sd">    :param batch_size: Number of attacks to run simultaneously.</span>
<span class="sd">    :param learning_rate: The learning rate for the attack algorithm.</span>
<span class="sd">                          Smaller values produce better results but are</span>
<span class="sd">                          slower to converge.</span>
<span class="sd">    :param binary_search_steps: The number of times we perform binary</span>
<span class="sd">                                search to find the optimal tradeoff-</span>
<span class="sd">                                constant between norm of the perturbation</span>
<span class="sd">                                and confidence of the classification. Set</span>
<span class="sd">                                &#39;initial_const&#39; to a large value and fix</span>
<span class="sd">                                this param to 1 for speed.</span>
<span class="sd">    :param max_iterations: The maximum number of iterations. Setting this</span>
<span class="sd">                           to a larger value will produce lower distortion</span>
<span class="sd">                           results. Using only a few iterations requires</span>
<span class="sd">                           a larger learning rate, and will produce larger</span>
<span class="sd">                           distortion results.</span>
<span class="sd">    :param abort_early: If true, allows early abort when the total</span>
<span class="sd">                        loss starts to increase (greatly speeds up attack,</span>
<span class="sd">                        but hurts performance, particularly on ImageNet)</span>
<span class="sd">    :param initial_const: The initial tradeoff-constant to use to tune the</span>
<span class="sd">                          relative importance of size of the perturbation</span>
<span class="sd">                          and confidence of classification.</span>
<span class="sd">                          If binary_search_steps is large, the initial</span>
<span class="sd">                          constant is not important. A smaller value of</span>
<span class="sd">                          this constant gives lower distortion results.</span>
<span class="sd">                          For computational efficiency, fix</span>
<span class="sd">                          binary_search_steps to 1 and set this param</span>
<span class="sd">                          to a large value.</span>
<span class="sd">    :param clip_min: (optional float) Minimum input component value</span>
<span class="sd">    :param clip_max: (optional float) Maximum input component value</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># ignore the y and y_target argument</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">beta</span> <span class="o">=</span> <span class="n">beta</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">decision_rule</span> <span class="o">=</span> <span class="n">decision_rule</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span> <span class="o">=</span> <span class="n">batch_size</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">confidence</span> <span class="o">=</span> <span class="n">confidence</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">learning_rate</span> <span class="o">=</span> <span class="n">learning_rate</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">binary_search_steps</span> <span class="o">=</span> <span class="n">binary_search_steps</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">max_iterations</span> <span class="o">=</span> <span class="n">max_iterations</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">abort_early</span> <span class="o">=</span> <span class="n">abort_early</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">initial_const</span> <span class="o">=</span> <span class="n">initial_const</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span> <span class="o">=</span> <span class="n">clip_min</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span> <span class="o">=</span> <span class="n">clip_max</span></div></div>


<span class="k">class</span> <span class="nc">EAD</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sess</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">decision_rule</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">,</span>
               <span class="n">confidence</span><span class="p">,</span> <span class="n">targeted</span><span class="p">,</span> <span class="n">learning_rate</span><span class="p">,</span> <span class="n">binary_search_steps</span><span class="p">,</span>
               <span class="n">max_iterations</span><span class="p">,</span> <span class="n">abort_early</span><span class="p">,</span> <span class="n">initial_const</span><span class="p">,</span> <span class="n">clip_min</span><span class="p">,</span>
               <span class="n">clip_max</span><span class="p">,</span> <span class="n">num_labels</span><span class="p">,</span> <span class="n">shape</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    EAD Attack</span>

<span class="sd">    Return a tensor that constructs adversarial examples for the given</span>
<span class="sd">    input. Generate uses tf.py_func in order to operate over tensors.</span>

<span class="sd">    :param sess: a TF session.</span>
<span class="sd">    :param model: a cleverhans.model.Model object.</span>
<span class="sd">    :param beta: Trades off L2 distortion with L1 distortion: higher</span>
<span class="sd">                 produces examples with lower L1 distortion, at the</span>
<span class="sd">                 cost of higher L2 (and typically Linf) distortion</span>
<span class="sd">    :param decision_rule: EN or L1. Select final adversarial example from</span>
<span class="sd">                          all successful examples based on the least</span>
<span class="sd">                          elastic-net or L1 distortion criterion.</span>
<span class="sd">    :param batch_size: Number of attacks to run simultaneously.</span>
<span class="sd">    :param confidence: Confidence of adversarial examples: higher produces</span>
<span class="sd">                       examples with larger l2 distortion, but more</span>
<span class="sd">                       strongly classified as adversarial.</span>
<span class="sd">    :param targeted: boolean controlling the behavior of the adversarial</span>
<span class="sd">                     examples produced. If set to False, they will be</span>
<span class="sd">                     misclassified in any wrong class. If set to True,</span>
<span class="sd">                     they will be misclassified in a chosen target class.</span>
<span class="sd">    :param learning_rate: The learning rate for the attack algorithm.</span>
<span class="sd">                          Smaller values produce better results but are</span>
<span class="sd">                          slower to converge.</span>
<span class="sd">    :param binary_search_steps: The number of times we perform binary</span>
<span class="sd">                                search to find the optimal tradeoff-</span>
<span class="sd">                                constant between norm of the perturbation</span>
<span class="sd">                                and confidence of the classification. Set</span>
<span class="sd">                                &#39;initial_const&#39; to a large value and fix</span>
<span class="sd">                                this param to 1 for speed.</span>
<span class="sd">    :param max_iterations: The maximum number of iterations. Setting this</span>
<span class="sd">                           to a larger value will produce lower distortion</span>
<span class="sd">                           results. Using only a few iterations requires</span>
<span class="sd">                           a larger learning rate, and will produce larger</span>
<span class="sd">                           distortion results.</span>
<span class="sd">    :param abort_early: If true, allows early abort when the total</span>
<span class="sd">                        loss starts to increase (greatly speeds up attack,</span>
<span class="sd">                        but hurts performance, particularly on ImageNet)</span>
<span class="sd">    :param initial_const: The initial tradeoff-constant to use to tune the</span>
<span class="sd">                          relative importance of size of the perturbation</span>
<span class="sd">                          and confidence of classification.</span>
<span class="sd">                          If binary_search_steps is large, the initial</span>
<span class="sd">                          constant is not important. A smaller value of</span>
<span class="sd">                          this constant gives lower distortion results.</span>
<span class="sd">                          For computational efficiency, fix</span>
<span class="sd">                          binary_search_steps to 1 and set this param</span>
<span class="sd">                          to a large value.</span>
<span class="sd">    :param clip_min: (optional float) Minimum input component value.</span>
<span class="sd">    :param clip_max: (optional float) Maximum input component value.</span>
<span class="sd">    :param num_labels: the number of classes in the model&#39;s output.</span>
<span class="sd">    :param shape: the shape of the model&#39;s input tensor.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">sess</span> <span class="o">=</span> <span class="n">sess</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">TARGETED</span> <span class="o">=</span> <span class="n">targeted</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">LEARNING_RATE</span> <span class="o">=</span> <span class="n">learning_rate</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">MAX_ITERATIONS</span> <span class="o">=</span> <span class="n">max_iterations</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">BINARY_SEARCH_STEPS</span> <span class="o">=</span> <span class="n">binary_search_steps</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">ABORT_EARLY</span> <span class="o">=</span> <span class="n">abort_early</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">CONFIDENCE</span> <span class="o">=</span> <span class="n">confidence</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">initial_const</span> <span class="o">=</span> <span class="n">initial_const</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span> <span class="o">=</span> <span class="n">batch_size</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span> <span class="o">=</span> <span class="n">clip_min</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span> <span class="o">=</span> <span class="n">clip_max</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">decision_rule</span> <span class="o">=</span> <span class="n">decision_rule</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">beta</span> <span class="o">=</span> <span class="n">beta</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">beta_t</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">beta</span><span class="p">,</span> <span class="n">tf_dtype</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">repeat</span> <span class="o">=</span> <span class="n">binary_search_steps</span> <span class="o">&gt;=</span> <span class="mi">10</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">shape</span> <span class="o">=</span> <span class="n">shape</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">([</span><span class="n">batch_size</span><span class="p">]</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="n">shape</span><span class="p">))</span>

    <span class="c1"># these are variables to be more efficient in sending data to tf</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">timg</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">shape</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf_dtype</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;timg&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">newimg</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span>
        <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">shape</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf_dtype</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;newimg&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">slack</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span>
        <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">shape</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf_dtype</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;slack&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">tlab</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span>
        <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">num_labels</span><span class="p">)),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf_dtype</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;tlab&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">const</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span>
        <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">batch_size</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf_dtype</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;const&#39;</span><span class="p">)</span>

    <span class="c1"># and here&#39;s what we use to assign them</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">assign_timg</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">placeholder</span><span class="p">(</span><span class="n">tf_dtype</span><span class="p">,</span> <span class="n">shape</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;assign_timg&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">assign_newimg</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">placeholder</span><span class="p">(</span>
        <span class="n">tf_dtype</span><span class="p">,</span> <span class="n">shape</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;assign_newimg&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">assign_slack</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">placeholder</span><span class="p">(</span>
        <span class="n">tf_dtype</span><span class="p">,</span> <span class="n">shape</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;assign_slack&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">assign_tlab</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">placeholder</span><span class="p">(</span>
        <span class="n">tf_dtype</span><span class="p">,</span> <span class="p">(</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">num_labels</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;assign_tlab&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">assign_const</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">placeholder</span><span class="p">(</span>
        <span class="n">tf_dtype</span><span class="p">,</span> <span class="p">[</span><span class="n">batch_size</span><span class="p">],</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;assign_const&#39;</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">global_step</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">trainable</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">global_step_t</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">global_step</span><span class="p">,</span> <span class="n">tf_dtype</span><span class="p">)</span>

    <span class="c1"># Fast Iterative Shrinkage Thresholding</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">zt</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">divide</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">global_step_t</span><span class="p">,</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">global_step_t</span> <span class="o">+</span> <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">tf_dtype</span><span class="p">))</span>
    <span class="n">cond1</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">greater</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slack</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">timg</span><span class="p">),</span>
                               <span class="bp">self</span><span class="o">.</span><span class="n">beta_t</span><span class="p">),</span> <span class="n">tf_dtype</span><span class="p">)</span>
    <span class="n">cond2</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">less_equal</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slack</span><span class="p">,</span>
                                                     <span class="bp">self</span><span class="o">.</span><span class="n">timg</span><span class="p">)),</span>
                                  <span class="bp">self</span><span class="o">.</span><span class="n">beta_t</span><span class="p">),</span> <span class="n">tf_dtype</span><span class="p">)</span>
    <span class="n">cond3</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">less</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slack</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">timg</span><span class="p">),</span>
                            <span class="n">tf</span><span class="o">.</span><span class="n">negative</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">beta_t</span><span class="p">)),</span> <span class="n">tf_dtype</span><span class="p">)</span>

    <span class="n">upper</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slack</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">beta_t</span><span class="p">),</span>
                       <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span><span class="p">,</span> <span class="n">tf_dtype</span><span class="p">))</span>
    <span class="n">lower</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slack</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">beta_t</span><span class="p">),</span>
                       <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span><span class="p">,</span> <span class="n">tf_dtype</span><span class="p">))</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">assign_newimg</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">multiply</span><span class="p">(</span><span class="n">cond1</span><span class="p">,</span> <span class="n">upper</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">assign_newimg</span> <span class="o">+=</span> <span class="n">tf</span><span class="o">.</span><span class="n">multiply</span><span class="p">(</span><span class="n">cond2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">timg</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">assign_newimg</span> <span class="o">+=</span> <span class="n">tf</span><span class="o">.</span><span class="n">multiply</span><span class="p">(</span><span class="n">cond3</span><span class="p">,</span> <span class="n">lower</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">assign_slack</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">assign_newimg</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">assign_slack</span> <span class="o">+=</span> <span class="n">tf</span><span class="o">.</span><span class="n">multiply</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">zt</span><span class="p">,</span>
                                     <span class="bp">self</span><span class="o">.</span><span class="n">assign_newimg</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">newimg</span><span class="p">)</span>

    <span class="c1"># --------------------------------</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">setter</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">newimg</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">assign_newimg</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">setter_y</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slack</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">assign_slack</span><span class="p">)</span>

    <span class="c1"># prediction BEFORE-SOFTMAX of the model</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">output</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_logits</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">newimg</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">output_y</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_logits</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slack</span><span class="p">)</span>

    <span class="c1"># distance to the input data</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">l2dist</span> <span class="o">=</span> <span class="n">reduce_sum</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">newimg</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">timg</span><span class="p">),</span>
                             <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">shape</span><span class="p">))))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">l2dist_y</span> <span class="o">=</span> <span class="n">reduce_sum</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slack</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">timg</span><span class="p">),</span>
                               <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">shape</span><span class="p">))))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">l1dist</span> <span class="o">=</span> <span class="n">reduce_sum</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">newimg</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">timg</span><span class="p">),</span>
                             <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">shape</span><span class="p">))))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">l1dist_y</span> <span class="o">=</span> <span class="n">reduce_sum</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slack</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">timg</span><span class="p">),</span>
                               <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">shape</span><span class="p">))))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">elasticdist</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">l2dist</span> <span class="o">+</span> <span class="n">tf</span><span class="o">.</span><span class="n">multiply</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">l1dist</span><span class="p">,</span>
                                                 <span class="bp">self</span><span class="o">.</span><span class="n">beta_t</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">elasticdist_y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">l2dist_y</span> <span class="o">+</span> <span class="n">tf</span><span class="o">.</span><span class="n">multiply</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">l1dist_y</span><span class="p">,</span>
                                                     <span class="bp">self</span><span class="o">.</span><span class="n">beta_t</span><span class="p">)</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">decision_rule</span> <span class="o">==</span> <span class="s1">&#39;EN&#39;</span><span class="p">:</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">crit</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">elasticdist</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">crit_p</span> <span class="o">=</span> <span class="s1">&#39;Elastic&#39;</span>
    <span class="k">else</span><span class="p">:</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">crit</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">l1dist</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">crit_p</span> <span class="o">=</span> <span class="s1">&#39;L1&#39;</span>

    <span class="c1"># compute the probability of the label class versus the maximum other</span>
    <span class="n">real</span> <span class="o">=</span> <span class="n">reduce_sum</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">tlab</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">output</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">real_y</span> <span class="o">=</span> <span class="n">reduce_sum</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">tlab</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">output_y</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">other</span> <span class="o">=</span> <span class="n">reduce_max</span><span class="p">((</span><span class="mi">1</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">tlab</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">output</span> <span class="o">-</span>
                       <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">tlab</span> <span class="o">*</span> <span class="mi">10000</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">other_y</span> <span class="o">=</span> <span class="n">reduce_max</span><span class="p">((</span><span class="mi">1</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">tlab</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">output_y</span> <span class="o">-</span>
                         <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">tlab</span> <span class="o">*</span> <span class="mi">10000</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>

    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">TARGETED</span><span class="p">:</span>
      <span class="c1"># if targeted, optimize for making the other class most likely</span>
      <span class="n">loss1</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">ZERO</span><span class="p">(),</span> <span class="n">other</span> <span class="o">-</span> <span class="n">real</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">CONFIDENCE</span><span class="p">)</span>
      <span class="n">loss1_y</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">ZERO</span><span class="p">(),</span> <span class="n">other_y</span> <span class="o">-</span> <span class="n">real_y</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">CONFIDENCE</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
      <span class="c1"># if untargeted, optimize for making this class least likely.</span>
      <span class="n">loss1</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">ZERO</span><span class="p">(),</span> <span class="n">real</span> <span class="o">-</span> <span class="n">other</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">CONFIDENCE</span><span class="p">)</span>
      <span class="n">loss1_y</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">ZERO</span><span class="p">(),</span> <span class="n">real_y</span> <span class="o">-</span> <span class="n">other_y</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">CONFIDENCE</span><span class="p">)</span>

    <span class="c1"># sum up the losses</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">loss21</span> <span class="o">=</span> <span class="n">reduce_sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">l1dist</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">loss21_y</span> <span class="o">=</span> <span class="n">reduce_sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">l1dist_y</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">loss2</span> <span class="o">=</span> <span class="n">reduce_sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">l2dist</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">loss2_y</span> <span class="o">=</span> <span class="n">reduce_sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">l2dist_y</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">loss1</span> <span class="o">=</span> <span class="n">reduce_sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">const</span> <span class="o">*</span> <span class="n">loss1</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">loss1_y</span> <span class="o">=</span> <span class="n">reduce_sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">const</span> <span class="o">*</span> <span class="n">loss1_y</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">loss_opt</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">loss1_y</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">loss2_y</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">loss1</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">loss2</span><span class="o">+</span><span class="n">tf</span><span class="o">.</span><span class="n">multiply</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">beta_t</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">loss21</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">learning_rate</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">polynomial_decay</span><span class="p">(</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">LEARNING_RATE</span><span class="p">,</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">global_step</span><span class="p">,</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">MAX_ITERATIONS</span><span class="p">,</span>
        <span class="mi">0</span><span class="p">,</span>
        <span class="n">power</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>

    <span class="c1"># Setup the optimizer and keep track of variables we&#39;re creating</span>
    <span class="n">start_vars</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">tf</span><span class="o">.</span><span class="n">global_variables</span><span class="p">())</span>
    <span class="n">optimizer</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">GradientDescentOptimizer</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">learning_rate</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">train</span> <span class="o">=</span> <span class="n">optimizer</span><span class="o">.</span><span class="n">minimize</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">loss_opt</span><span class="p">,</span>
                                    <span class="n">var_list</span><span class="o">=</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">slack</span><span class="p">],</span>
                                    <span class="n">global_step</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">global_step</span><span class="p">)</span>
    <span class="n">end_vars</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">global_variables</span><span class="p">()</span>
    <span class="n">new_vars</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">end_vars</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">start_vars</span><span class="p">]</span>

    <span class="c1"># these are the variables to initialize when we run</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">setup</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">setup</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">timg</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">assign_timg</span><span class="p">))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">setup</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">tlab</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">assign_tlab</span><span class="p">))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">setup</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">const</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">assign_const</span><span class="p">))</span>

    <span class="n">var_list</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">global_step</span><span class="p">]</span><span class="o">+</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">slack</span><span class="p">]</span><span class="o">+</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">newimg</span><span class="p">]</span><span class="o">+</span><span class="n">new_vars</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">init</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">variables_initializer</span><span class="p">(</span><span class="n">var_list</span><span class="o">=</span><span class="n">var_list</span><span class="p">)</span>

  <span class="k">def</span> <span class="nf">attack</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">imgs</span><span class="p">,</span> <span class="n">targets</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Perform the EAD attack on the given instance for the given targets.</span>

<span class="sd">    If self.targeted is true, then the targets represents the target labels</span>
<span class="sd">    If self.targeted is false, then targets are the original class labels</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">batch_size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span>
    <span class="n">r</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">imgs</span><span class="p">)</span> <span class="o">//</span> <span class="n">batch_size</span><span class="p">):</span>
      <span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
          <span class="p">(</span><span class="s2">&quot;Running EAD attack on instance </span><span class="si">%s</span><span class="s2"> of </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span>
           <span class="n">i</span> <span class="o">*</span> <span class="n">batch_size</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">imgs</span><span class="p">)))</span>
      <span class="n">r</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">attack_batch</span><span class="p">(</span>
              <span class="n">imgs</span><span class="p">[</span><span class="n">i</span> <span class="o">*</span> <span class="n">batch_size</span><span class="p">:(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">batch_size</span><span class="p">],</span>
              <span class="n">targets</span><span class="p">[</span><span class="n">i</span> <span class="o">*</span> <span class="n">batch_size</span><span class="p">:(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">batch_size</span><span class="p">]))</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">imgs</span><span class="p">)</span> <span class="o">%</span> <span class="n">batch_size</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
      <span class="n">last_elements</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">imgs</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">imgs</span><span class="p">)</span> <span class="o">%</span> <span class="n">batch_size</span><span class="p">)</span>
      <span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
          <span class="p">(</span><span class="s2">&quot;Running EAD attack on instance </span><span class="si">%s</span><span class="s2"> of </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span>
           <span class="n">last_elements</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">imgs</span><span class="p">)))</span>
      <span class="n">temp_imgs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">batch_size</span><span class="p">,</span> <span class="p">)</span> <span class="o">+</span> <span class="n">imgs</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">:])</span>
      <span class="n">temp_targets</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">batch_size</span><span class="p">,</span> <span class="p">)</span> <span class="o">+</span> <span class="n">targets</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">:])</span>
      <span class="n">temp_imgs</span><span class="p">[:(</span><span class="nb">len</span><span class="p">(</span><span class="n">imgs</span><span class="p">)</span> <span class="o">%</span> <span class="n">batch_size</span><span class="p">)]</span> <span class="o">=</span> <span class="n">imgs</span><span class="p">[</span><span class="n">last_elements</span><span class="p">:]</span>
      <span class="n">temp_targets</span><span class="p">[:(</span><span class="nb">len</span><span class="p">(</span><span class="n">imgs</span><span class="p">)</span> <span class="o">%</span> <span class="n">batch_size</span><span class="p">)]</span> <span class="o">=</span> <span class="n">targets</span><span class="p">[</span><span class="n">last_elements</span><span class="p">:]</span>
      <span class="n">temp_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">attack_batch</span><span class="p">(</span><span class="n">temp_imgs</span><span class="p">,</span> <span class="n">temp_targets</span><span class="p">)</span>
      <span class="n">r</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">temp_data</span><span class="p">[:(</span><span class="nb">len</span><span class="p">(</span><span class="n">imgs</span><span class="p">)</span> <span class="o">%</span> <span class="n">batch_size</span><span class="p">)],</span>
               <span class="n">targets</span><span class="p">[</span><span class="n">last_elements</span><span class="p">:])</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>

  <span class="k">def</span> <span class="nf">attack_batch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">imgs</span><span class="p">,</span> <span class="n">labs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Run the attack on a batch of instance and labels.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">compare</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
      <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="p">(</span><span class="nb">float</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">int64</span><span class="p">)):</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">TARGETED</span><span class="p">:</span>
          <span class="n">x</span><span class="p">[</span><span class="n">y</span><span class="p">]</span> <span class="o">-=</span> <span class="bp">self</span><span class="o">.</span><span class="n">CONFIDENCE</span>
        <span class="k">else</span><span class="p">:</span>
          <span class="n">x</span><span class="p">[</span><span class="n">y</span><span class="p">]</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">CONFIDENCE</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">TARGETED</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">x</span> <span class="o">==</span> <span class="n">y</span>
      <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">x</span> <span class="o">!=</span> <span class="n">y</span>

    <span class="n">batch_size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span>

    <span class="n">imgs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">clip</span><span class="p">(</span><span class="n">imgs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span><span class="p">)</span>

    <span class="c1"># set the lower and upper bounds accordingly</span>
    <span class="n">lower_bound</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">batch_size</span><span class="p">)</span>
    <span class="n">CONST</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">batch_size</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_const</span>
    <span class="n">upper_bound</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">batch_size</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1e10</span>

    <span class="c1"># placeholders for the best en, score, and instance attack found so far</span>
    <span class="n">o_bestdst</span> <span class="o">=</span> <span class="p">[</span><span class="mf">1e10</span><span class="p">]</span> <span class="o">*</span> <span class="n">batch_size</span>
    <span class="n">o_bestscore</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">batch_size</span>
    <span class="n">o_bestattack</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">imgs</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">outer_step</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">BINARY_SEARCH_STEPS</span><span class="p">):</span>
      <span class="c1"># completely reset the optimizer&#39;s internal state.</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">init</span><span class="p">)</span>
      <span class="n">batch</span> <span class="o">=</span> <span class="n">imgs</span><span class="p">[:</span><span class="n">batch_size</span><span class="p">]</span>
      <span class="n">batchlab</span> <span class="o">=</span> <span class="n">labs</span><span class="p">[:</span><span class="n">batch_size</span><span class="p">]</span>

      <span class="n">bestdst</span> <span class="o">=</span> <span class="p">[</span><span class="mf">1e10</span><span class="p">]</span> <span class="o">*</span> <span class="n">batch_size</span>
      <span class="n">bestscore</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">batch_size</span>
      <span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;  Binary search step </span><span class="si">%s</span><span class="s2"> of </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span>
                    <span class="n">outer_step</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">BINARY_SEARCH_STEPS</span><span class="p">)</span>

      <span class="c1"># The last iteration (if we run many steps) repeat the search once.</span>
      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">repeat</span> <span class="ow">and</span> <span class="n">outer_step</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">BINARY_SEARCH_STEPS</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">CONST</span> <span class="o">=</span> <span class="n">upper_bound</span>

      <span class="c1"># set the variables so that we don&#39;t have to send them over again</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">setup</span><span class="p">,</span> <span class="p">{</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">assign_timg</span><span class="p">:</span> <span class="n">batch</span><span class="p">,</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">assign_tlab</span><span class="p">:</span> <span class="n">batchlab</span><span class="p">,</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">assign_const</span><span class="p">:</span> <span class="n">CONST</span>
          <span class="p">})</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">setter</span><span class="p">,</span> <span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">assign_newimg</span><span class="p">:</span> <span class="n">batch</span><span class="p">})</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">setter_y</span><span class="p">,</span> <span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">assign_slack</span><span class="p">:</span> <span class="n">batch</span><span class="p">})</span>
      <span class="n">prev</span> <span class="o">=</span> <span class="mf">1e6</span>
      <span class="k">for</span> <span class="n">iteration</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">MAX_ITERATIONS</span><span class="p">):</span>
        <span class="c1"># perform the attack</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">train</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">setter</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">setter_y</span><span class="p">])</span>
        <span class="n">l</span><span class="p">,</span> <span class="n">l2s</span><span class="p">,</span> <span class="n">l1s</span><span class="p">,</span> <span class="n">crit</span><span class="p">,</span> <span class="n">scores</span><span class="p">,</span> <span class="n">nimg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">loss</span><span class="p">,</span>
                                                         <span class="bp">self</span><span class="o">.</span><span class="n">l2dist</span><span class="p">,</span>
                                                         <span class="bp">self</span><span class="o">.</span><span class="n">l1dist</span><span class="p">,</span>
                                                         <span class="bp">self</span><span class="o">.</span><span class="n">crit</span><span class="p">,</span>
                                                         <span class="bp">self</span><span class="o">.</span><span class="n">output</span><span class="p">,</span>
                                                         <span class="bp">self</span><span class="o">.</span><span class="n">newimg</span><span class="p">])</span>
        <span class="k">if</span> <span class="n">iteration</span> <span class="o">%</span> <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">MAX_ITERATIONS</span> <span class="o">//</span> <span class="mi">10</span><span class="p">)</span> <span class="ow">or</span> <span class="mi">1</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
          <span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">((</span><span class="s2">&quot;    Iteration </span><span class="si">{}</span><span class="s2"> of </span><span class="si">{}</span><span class="s2">: loss=</span><span class="si">{:.3g}</span><span class="s2"> &quot;</span> <span class="o">+</span>
                         <span class="s2">&quot;l2=</span><span class="si">{:.3g}</span><span class="s2"> l1=</span><span class="si">{:.3g}</span><span class="s2"> f=</span><span class="si">{:.3g}</span><span class="s2">&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                             <span class="n">iteration</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">MAX_ITERATIONS</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span>
                             <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">l2s</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">l1s</span><span class="p">),</span>
                             <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">scores</span><span class="p">)))</span>

        <span class="c1"># check if we should abort search if we&#39;re getting nowhere.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ABORT_EARLY</span> <span class="ow">and</span> \
           <span class="n">iteration</span> <span class="o">%</span> <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">MAX_ITERATIONS</span> <span class="o">//</span> <span class="mi">10</span><span class="p">)</span> <span class="ow">or</span> <span class="mi">1</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
          <span class="k">if</span> <span class="n">l</span> <span class="o">&gt;</span> <span class="n">prev</span> <span class="o">*</span> <span class="o">.</span><span class="mi">9999</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;    Failed to make progress; stop early&quot;</span>
            <span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="k">break</span>
          <span class="n">prev</span> <span class="o">=</span> <span class="n">l</span>

        <span class="c1"># adjust the best result found so far</span>
        <span class="k">for</span> <span class="n">e</span><span class="p">,</span> <span class="p">(</span><span class="n">dst</span><span class="p">,</span> <span class="n">sc</span><span class="p">,</span> <span class="n">ii</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">crit</span><span class="p">,</span> <span class="n">scores</span><span class="p">,</span> <span class="n">nimg</span><span class="p">)):</span>
          <span class="n">lab</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">batchlab</span><span class="p">[</span><span class="n">e</span><span class="p">])</span>
          <span class="k">if</span> <span class="n">dst</span> <span class="o">&lt;</span> <span class="n">bestdst</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="ow">and</span> <span class="n">compare</span><span class="p">(</span><span class="n">sc</span><span class="p">,</span> <span class="n">lab</span><span class="p">):</span>
            <span class="n">bestdst</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">=</span> <span class="n">dst</span>
            <span class="n">bestscore</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">sc</span><span class="p">)</span>
          <span class="k">if</span> <span class="n">dst</span> <span class="o">&lt;</span> <span class="n">o_bestdst</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="ow">and</span> <span class="n">compare</span><span class="p">(</span><span class="n">sc</span><span class="p">,</span> <span class="n">lab</span><span class="p">):</span>
            <span class="n">o_bestdst</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">=</span> <span class="n">dst</span>
            <span class="n">o_bestscore</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">sc</span><span class="p">)</span>
            <span class="n">o_bestattack</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">=</span> <span class="n">ii</span>

      <span class="c1"># adjust the constant as needed</span>
      <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">batch_size</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">compare</span><span class="p">(</span><span class="n">bestscore</span><span class="p">[</span><span class="n">e</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">batchlab</span><span class="p">[</span><span class="n">e</span><span class="p">]))</span> <span class="ow">and</span> \
           <span class="n">bestscore</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
          <span class="c1"># success, divide const by two</span>
          <span class="n">upper_bound</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">upper_bound</span><span class="p">[</span><span class="n">e</span><span class="p">],</span> <span class="n">CONST</span><span class="p">[</span><span class="n">e</span><span class="p">])</span>
          <span class="k">if</span> <span class="n">upper_bound</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mf">1e9</span><span class="p">:</span>
            <span class="n">CONST</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">lower_bound</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">+</span> <span class="n">upper_bound</span><span class="p">[</span><span class="n">e</span><span class="p">])</span> <span class="o">/</span> <span class="mi">2</span>
        <span class="k">else</span><span class="p">:</span>
          <span class="c1"># failure, either multiply by 10 if no solution found yet</span>
          <span class="c1">#          or do binary search with the known upper bound</span>
          <span class="n">lower_bound</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">lower_bound</span><span class="p">[</span><span class="n">e</span><span class="p">],</span> <span class="n">CONST</span><span class="p">[</span><span class="n">e</span><span class="p">])</span>
          <span class="k">if</span> <span class="n">upper_bound</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mf">1e9</span><span class="p">:</span>
            <span class="n">CONST</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">lower_bound</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">+</span> <span class="n">upper_bound</span><span class="p">[</span><span class="n">e</span><span class="p">])</span> <span class="o">/</span> <span class="mi">2</span>
          <span class="k">else</span><span class="p">:</span>
            <span class="n">CONST</span><span class="p">[</span><span class="n">e</span><span class="p">]</span> <span class="o">*=</span> <span class="mi">10</span>
      <span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;  Successfully generated adversarial examples &quot;</span> <span class="o">+</span>
                    <span class="s2">&quot;on </span><span class="si">{}</span><span class="s2"> of </span><span class="si">{}</span><span class="s2"> instances.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="nb">sum</span><span class="p">(</span><span class="n">upper_bound</span> <span class="o">&lt;</span> <span class="mf">1e9</span><span class="p">),</span> <span class="n">batch_size</span><span class="p">))</span>
      <span class="n">o_bestdst</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">o_bestdst</span><span class="p">)</span>
      <span class="n">mean</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">o_bestdst</span><span class="p">[</span><span class="n">o_bestdst</span> <span class="o">&lt;</span> <span class="mf">1e9</span><span class="p">]))</span>
      <span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">crit_p</span> <span class="o">+</span>
                    <span class="s2">&quot; Mean successful distortion: </span><span class="si">{:.4g}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">mean</span><span class="p">))</span>

    <span class="c1"># return the best solution found</span>
    <span class="n">o_bestdst</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">o_bestdst</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">o_bestattack</span>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../../index.html">CleverHans</a></h1>








<h3>Navigation</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../source/attacks.html"><cite>attacks</cite> module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../source/model.html"><cite>model</cite> module</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../index.html">Documentation overview</a><ul>
  <li><a href="../../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>


  </body>
</html>